<?php

namespace App\Http\Controllers;

use Auth;
use App\Models\Role;
use App\Models\Permission;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\Role\CreateForm;
use App\Http\Requests\Role\UpdateForm;

class RoleController extends Controller
{
    /**
     * @var Role
     */
    private $role;

    private $permission;

    public function __construct(Role $role,Permission $permission)
    {
        $this->role = $role;
        $this->permission = $permission;
    }

    public function index(){
        $this->validateUserPermissionRedirect('show-roles');
        $roles = Role::paginate();
        return view('role.index',compact('roles'));
    }

    public function create(){
        $this->validateUserPermissionRedirect('create-role');
        $permissionLevel1 = $this->permission->getChildPermissions();
        return view('role.create',compact('permissionLevel1'));
    }

    public function store(CreateForm $request){
        $this->validateUserPermissionRedirect('create-role');
        $role = $this->role;
        $role->name = $request->name;
        $role->cn_name = $request->cn_name;

        DB::beginTransaction();

        try{
            $role->save();
            $role->givePermissionTo($request->permissions);
            DB::commit();
            session()->flash('success','创建'.$request->name.'角色成功');
            return  redirect()->route('role.index');
        }catch (\Exception $exception){
            DB::rollBack();
            session()->flash('danger','创建'.$request->name.'角色失败');
            return  redirect()->back();
        }

    }

    public function edit( $id ){
        $this->validateUserPermissionRedirect('edit-role');
        $role = $this->role->find($id);
        $permissionGroup = $this->permission->getPermissionGroups();
        return view('role.edit',compact('role','permissionGroup'));
    }

    public function update(UpdateForm $request , $id){
        $this->validateUserPermissionRedirect('edit-role');
        $role = $this->role->find($id);

        if($role->name != 'SuperManager'){

            DB::beginTransaction();

            try{
                $role->name = $request->name;
                $role->cn_name = $request->cn_name;
                $role->save();

                if($request->permissions){
                    $role->syncPermissions($request->permissions);
                }else{
                    $rolePermissions = $role->permissions;
                    foreach ($rolePermissions as $permission){
                        $role->revokePermissionTo($permission->name);
                    }
                }

                DB::commit();
                session()->flash('success','修改'.$request->name.'角色成功');
                return  redirect()->route('role.index');
            }catch (\Exception $exception){
                DB::rollBack();
                //throw $exception;
                session()->flash('danger','修改'.$request->name.'角色失败');
                return  redirect()->route('role.index');
            }
        }
        session()->flash('danger','修改'.$request->name.'角色失败，不允许操作此角色');
        return  redirect()->route('role.index');

    }

    public function destroy( $id ){
        if(Auth::user()->can('delete-role') || Auth::user()->hasRole('SuperManager')){
            $role = $this->role->find($id);
            if($role->delete()){
                session()->flash('success','删除'.$role->cn_name.'角色成功');
                return  redirect()->route('role.index');
            }
        }else{
            abort(401);
        }
    }

}
